﻿using System.Threading.Tasks;
using WeiboWPSdk.Json;
using WeiboWPSdk.Json.Linq;
using WeiboWPSdk.ResponseEntity;

namespace WeiboWPSdk.Api {
    public class FavoriteApi:ApiBase {
        public FavoriteApi(ClientHttpHelper httpHelper)
            : base(httpHelper) { 
        }

        #region 读取接口
        /// <summary>
        /// 获取当前登录用户的收藏列表
        /// http://open.weibo.com/wiki/2/favorites
        /// </summary>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns></returns>
        public async Task<FavoriteCollection> GetAsync(int count = 50, int page = 1) {
            var json = await HttpHelper.GetBasicCommandAsync("favorites", new WeiboParameter("count", count), new WeiboParameter("page", page));
            return JsonConvert.DeserializeObject<FavoriteCollection>(json);
        }
        /// <summary>
        /// 获取当前用户的收藏列表的ID
        /// http://open.weibo.com/wiki/2/favorites/ids
        /// </summary>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns></returns>
        public async Task<FavoriteIdCollection> GetIdsAsync(int count = 50, int page = 1) {
            var json = await HttpHelper.GetBasicCommandAsync("favorites/ids", new WeiboParameter("count", count), new WeiboParameter("page", page));
            return JsonConvert.DeserializeObject<FavoriteIdCollection>(json);
        }
        /// <summary>
        /// 根据收藏ID获取指定的收藏信息
        /// http://open.weibo.com/wiki/2/favorites/show
        /// </summary>
        /// <param name="id">需要查询的收藏ID。</param>
        /// <returns></returns>
        public async Task<Favorite> ShowAsync(long id) {
            var json = await HttpHelper.GetBasicCommandAsync("favorites/show", new WeiboParameter("id", id));
            return JsonConvert.DeserializeObject<Favorite>(json);
        }
        /// <summary>
        /// 根据标签获取当前登录用户该标签下的收藏列表
        /// http://open.weibo.com/wiki/2/favorites/by_tags
        /// </summary>
        /// <param name="tid">需要查询的标签ID。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns></returns>
        public async Task<FavoriteCollection> GetByTagsAsync(long tid, int count = 50, int page = 1) {
            var json = await HttpHelper.GetBasicCommandAsync("favorites/by_tags", new WeiboParameter("tid", tid), new WeiboParameter("count", count), new WeiboParameter("page", page));
            return JsonConvert.DeserializeObject<FavoriteCollection>(json);
        }
        /// <summary>
        /// 获取当前登录用户的收藏标签列表
        /// http://open.weibo.com/wiki/2/favorites/tags
        /// </summary>
        /// <param name="count">单页返回的记录条数，默认为10。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns></returns>
        public async Task<TagCollection> GetTagsAsync(int count = 10, int page = 1) {
            var json = await HttpHelper.GetBasicCommandAsync("favorites/tags", new WeiboParameter("count", count), new WeiboParameter("page", page));
            return JsonConvert.DeserializeObject<TagCollection>(json);
        }
        /// <summary>
        /// 获取当前用户某个标签下的收藏列表的ID
        /// http://open.weibo.com/wiki/2/favorites/by_tags/ids
        /// </summary>
        /// <param name="tid">需要查询的标签ID。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <returns></returns>
        public async Task<FavoriteIdCollection> GetIdsByTagsAsync(long tid, int count = 50, int page = 1) {
            var json = await HttpHelper.GetBasicCommandAsync("favorites/by_tags/ids", new WeiboParameter("tid", tid), new WeiboParameter("count", count), new WeiboParameter("page", page));
            return JsonConvert.DeserializeObject<FavoriteIdCollection>(json);
        }
        #endregion
        #region 写入接口
        /// <summary>
        /// 添加一条微博到收藏里
        /// http://open.weibo.com/wiki/2/favorites/create
        /// </summary>
        /// <param name="id">要收藏的微博ID。</param>
        /// <returns></returns>
        public async Task<Favorite> CreateAsync(long id) {
            var json = await HttpHelper.PostBasicCommandAsync("favorites/create", new WeiboParameter("id", id));
            return JsonConvert.DeserializeObject<Favorite>(json);
        }
        /// <summary>
        /// 取消收藏一条微博
        /// http://open.weibo.com/wiki/2/favorites/destroy
        /// </summary>
        /// <param name="id">要取消收藏的微博ID。</param>
        /// <returns></returns>
        public async Task<Favorite> DestroyAsync(long id) {
            var json = await HttpHelper.PostBasicCommandAsync("favorites/destroy", new WeiboParameter("id", id));
            return JsonConvert.DeserializeObject<Favorite>(json);
        }
        /// <summary>
        /// 根据收藏ID批量取消收藏
        /// http://open.weibo.com/wiki/2/favorites/destroy_batch
        /// </summary>
        /// <param name="ids">要取消收藏的收藏ID，用半角逗号分隔，最多不超过10个。</param>
        /// <returns></returns>
        public async Task<bool> DestroyBatchAsync(params long[] ids) {
            var json = await HttpHelper.PostBasicCommandAsync("favorites/destroy_batch", new WeiboParameter("ids", string.Join(",", ids)));
            return (bool)JObject.Parse(json)["result"];
        }
        /// <summary>
        /// 更新一条收藏的收藏标签
        /// http://open.weibo.com/wiki/2/favorites/tags/update
        /// 参数tags为空即为删除标签
        /// </summary>
        /// <param name="id">需要更新的收藏ID。</param>
        /// <param name="tags">需要更新的标签内容，必须做URLencode，用半角逗号分隔，最多不超过2条。</param>
        /// <returns></returns>
        public async Task<Favorite> UpdateTagsAsync(long id, params string[] tags) {
            var json = await HttpHelper.PostBasicCommandAsync("favorites/tags/update", new WeiboParameter("id", id), new WeiboParameter("tags", string.Join(",", tags)));
            return JsonConvert.DeserializeObject<Favorite>(json);
        }
        /// <summary>
        /// 更新当前登录用户所有收藏下的指定标签
        /// http://open.weibo.com/wiki/2/favorites/tags/update_batch
        /// </summary>
        /// <param name="tid">需要更新的标签ID。</param>
        /// <param name="tag">需要更新的标签内容，必须做URLencode。</param>
        /// <returns></returns>
        public async Task<Tag> BatchUpdateTagsAsync(long tid, string tag) {
            var json = await HttpHelper.PostBasicCommandAsync("favorites/tags/update_batch", new WeiboParameter("tid", tid), new WeiboParameter("tag", tag));
            return JsonConvert.DeserializeObject<Tag>(json);
        }
        /// <summary>
        /// 删除当前登录用户所有收藏下的指定标签
        /// http://open.weibo.com/wiki/2/favorites/tags/destroy_batch
        /// 删除标签后，该用户所有收藏中，添加了该标签的收藏均解除与该标签的关联关系
        /// </summary>
        /// <param name="tid">需要删除的标签ID。</param>
        /// <returns></returns>
        public async Task<bool> BatchDestroyTagsAsync(long tid) {
            var json = await HttpHelper.PostBasicCommandAsync("favorites/tags/destroy_batch", new WeiboParameter("tid", tid));
            return (bool)JObject.Parse(json)["result"];
        }
        #endregion
    }
}
